package org.aplikator.server.descriptor;

import java.io.Serializable;

import org.aplikator.client.descriptor.ComboBoxDTO;
import org.aplikator.client.descriptor.WidgetDTO;
import org.aplikator.server.Context;

public class ComboBox <T extends Serializable> implements Widget, HasProperty<T> {

    private Property<T> property;

    private boolean labelAbove = true;

    public ComboBox(Property<T> property) {
        this.property = property;
        if (property.getListProvider() == null) {
            throw new IllegalStateException("Property " + property.getId() + " must have assigned listProvider");
        }
    }

    public Property<T> getProperty() {
        return property;
    }

    public WidgetDTO getWidgetDescriptor(Context ctx) {
        ComboBoxDTO<T> desc = new ComboBoxDTO<T>(property.clientClone(ctx));
        desc.setLabelAbove(labelAbove);
        return desc;
    }

    public void registerProperties(Form form) {
        form.addProperty(property);
    }

    public boolean isLabelAbove() {
        return labelAbove;
    }

    public ComboBox<T> setLabelAbove(boolean labelAbove) {
        this.labelAbove = labelAbove;
        return this;
    }
}
